home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume20 / cfortran / part01 next >
Encoding:
Text File  |  1991-06-26  |  44.7 KB  |  1,285 lines

  1. Newsgroups: comp.sources.misc
  2. From: Burkhard Burow <burow@cernvax.cern.ch>
  3. Subject:  v20i066:  cfortran - a bridge between C and FORTRAN, Part01/02
  4. Message-ID: <csm-v20i066=cfortran.143251@sparky.imd.sterling.com>
  5. X-Md4-Signature: eab6a0e5de48dcc9c9b7d76539c89457
  6. Date: Tue, 25 Jun 1991 19:33:58 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: Burkhard Burow <burow@cernvax.cern.ch>
  10. Posting-number: Volume 20, Issue 66
  11. Archive-name: cfortran/part01
  12. Environment: FORTRAN, MIPS, AIX, SGI, DEC 
  13.  
  14. Utility name:    cfortran.h
  15. Source language: C
  16. Purpose:         Help make mixing C and FORTRAN easy and machine independant.
  17. Machines/OS: MIPS cc and f77 2.0. (e.g. Silicon Graphics, DECstations, ...)
  18.              IBM AIX XL C and FORTRAN Compiler/6000 Version 01.01.0000.0000
  19.              VAX VMS CC 3.1 and FORTRAN 5.4.
  20.  
  21. The following contains:
  22. cfortran.doc - explaining how to use cfortran.h
  23. cfortran.h   - a C header file which
  24.                i)  allows for easy 'prototyping' of FORTRAN functions, so that
  25.                    they may be called from C.
  26.                ii) helps generate C wrappers for C functions so that the
  27.                    functions may be called from FORTRAN.
  28. cfortest.c
  29. cfortex.for  - the source code to many simple demos of cfortran.h in action.
  30.  
  31. burkhard       burow@cernvax.cern.ch
  32. ---
  33. #! /bin/sh
  34. # This is a shell archive.  Remove anything before this line, then feed it
  35. # into a shell via "sh file" or similar.  To overwrite existing files,
  36. # type "sh file -c".
  37. # The tool that generated this appeared in the comp.sources.unix newsgroup;
  38. # send mail to comp-sources-unix@uunet.uu.net if you want that tool.
  39. # Contents:  cfortest.c cfortex.for cfortran.doc
  40. # Wrapped by kent@sparky on Tue Jun 25 14:25:33 1991
  41. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  42. echo If this archive is complete, you will see the following message:
  43. echo '          "shar: End of archive 1 (of 2)."'
  44. if test -f 'cfortest.c' -a "${1}" != "-c" ; then 
  45.   echo shar: Will not clobber existing file \"'cfortest.c'\"
  46. else
  47.   echo shar: Extracting \"'cfortest.c'\" \(8968 characters\)
  48.   sed "s/^X//" >'cfortest.c' <<'END_OF_FILE'
  49. X/* cfortex.c */
  50. X/* Burkhard Burow, burow%13313.hepnet@csa3.lbl.gov, U. of Toronto, 1991. */
  51. X
  52. X#include <stdio.h>
  53. X#include "cfortran.h"
  54. X
  55. X#define   EQ_SELECT 1   /* To see the various examples select one of:
  56. X        EASY_SELECT,  ST_SELECT, FT_SELECT, S1_SELECT, ABC_SELECT,  R_SELECT,
  57. X         REV_SELECT, FCB_SELECT, EQ_SELECT, F0_SELECT,  FA_SELECT, FB_SELECT,
  58. X          FC_SELECT,  FD_SELECT, FE_SELECT, FF_SELECT,  FG_SELECT, FH_SELECT,
  59. X          FI_SELECT,  FJ_SELECT. */
  60. X
  61. X#ifdef EASY_SELECT
  62. XPROTOCCALLSFSUB2(easy,PINT,INT)
  63. X#define EASY(A,B)      CCALLSFSUB2(easy,PINT,INT, A,B)
  64. X
  65. Xmain() {
  66. Xint a;
  67. Xprintf("\nEASY EXAMPLE\n");
  68. XEASY(a,7);
  69. Xprintf("The FORTRAN routine easy(a,7) returns a = %d\n", a);
  70. X}
  71. X#endif
  72. X
  73. X#ifdef ST_SELECT
  74. XPROTOCCALLSFSUB3(st,PSTRINGV,STRINGV,FLOAT)
  75. X#define ST(A,B,C) CCALLSFSUB3(st,PSTRINGV,STRINGV,FLOAT,A,B,C)
  76. X
  77. Xint main() {
  78. Xstatic char v[][5] = {"000 ", "1", "22", " "};
  79. Xstatic char w[][9]  = {" ", "bb","ccc ","dddd"};
  80. XST(v, w, 10.);
  81. Xprintf("main:v=%s,%s,%s,%s. PSTRINGV => Has had trailing blanks stripped.\n",
  82. X       v[0],v[1],v[2],v[3]);
  83. Xprintf("main:w=%s,%s,%s,%s. STRINGV => malloc'd copy for FORTRAN=> C intact.\n"
  84. X       ,w[0],w[1],w[2],w[3]);
  85. X}
  86. X#endif
  87. X
  88. X#ifdef FT_SELECT
  89. XPROTOCCALLSFFUN3(STRING,ft,PSTRINGV,STRINGV,FLOAT)
  90. X#define FT(A,B,C) CCALLSFFUN3(ft,PSTRINGV,STRINGV,FLOAT,A,B,C)
  91. X
  92. Xmain() {
  93. Xstatic char v[][5] = {"000 ", "1", "22", " "};
  94. Xstatic char w[][9]  = {" ", "bb","ccc ","dddd"};
  95. Xfloat a = 10.0;
  96. Xprintf("FT(v, w, a); returns:%s.\n",FT(v, w, a));
  97. Xprintf("main:v=%s,%s,%s,%s. PSTRINGV => Has had trailing blanks stripped.\n",
  98. X       v[0],v[1],v[2],v[3]);
  99. Xprintf("main:w=%s,%s,%s,%s. STRINGV => malloc'd copy for FORTRAN=> C intact.\n"
  100. X       ,w[0],w[1],w[2],w[3]);
  101. X}
  102. X#endif
  103. X
  104. X#ifdef S1_SELECT
  105. XPROTOCCALLSFSUB1(s1,PSTRING)
  106. X#define S1(A1)              CCALLSFSUB1(s1,PSTRING,A1)
  107. XPROTOCCALLSFSUB1(forstr1,PSTRING)
  108. X#define FORSTR1(A1)         CCALLSFSUB1(forstr1,PSTRING,A1)
  109. X
  110. Xmain() {
  111. Xstatic char b[] = "abcdefghij", forb[13] = "abcdefghijkl";
  112. XS1(b); FORSTR1(forb);
  113. Xprintf("s1(b) returns b = %s; forstr1(forb) = returns forb = %s;\n", b, forb);
  114. X}
  115. X#endif
  116. X
  117. X#ifdef ABC_SELECT
  118. XPROTOCCALLSFSUB3(abc,STRING,PSTRING,PSTRING)
  119. X#define ABC(A1,A2,A3)       CCALLSFSUB3(abc,STRING,PSTRING,PSTRING,A1,A2,A3)
  120. X
  121. Xmain() {
  122. Xstatic char aa[] = "one  ", bb[] = "two  ", cc[] = "three"; int i;
  123. Xfor (i=0; i<10; i++) {printf("%s;%s;%s;\n",aa,bb,cc); ABC(aa,bb,cc);}
  124. X}
  125. X#endif
  126. X
  127. X#ifdef R_SELECT
  128. XPROTOCCALLSFFUN1(FLOAT,r,INT)
  129. X#define R(A1)               CCALLSFFUN1(r,INT,A1)
  130. XPROTOCCALLSFFUN0(STRING,forstr2)
  131. X#define FORSTR2()           CCALLSFFUN0(forstr2)
  132. XPROTOCCALLSFFUN1(STRING,forstr,STRING)
  133. X#define FORSTR(A1)          CCALLSFFUN1(forstr,STRING,A1)
  134. X
  135. Xmain() {
  136. Xstatic char aa[] = "one";
  137. Xint rrr = 333;
  138. Xprintf("R(rrr=%d) returns int arg. as float:%f\n",rrr,R(rrr));
  139. Xprintf("FORSTR(aa=%s) returns the string arg. as:%s<-end here\n",aa,FORSTR(aa));
  140. Xprintf("FORSTR2() returns the string constant:%s<-end here\n",FORSTR2());
  141. X}
  142. X#endif
  143. X
  144. X#ifdef REV_SELECT
  145. XPROTOCCALLSFFUN1(INT,frev,INTV)
  146. X#define FREV(A1)               CCALLSFFUN1(frev,INTV,A1)
  147. XPROTOCCALLSFSUB1(rev,INTV)
  148. X#define REV(A1)                CCALLSFSUB1(rev,INTV,A1)
  149. X
  150. Xmain() {
  151. Xstatic int a[] = {1,2};
  152. Xprintf("REV(a[0,1]=%d,%d) returns:",a[0],a[1]);
  153. XREV(a); printf("a[0,1]=%d,%d\n",a[0],a[1]);
  154. Xprintf("FREV(a[0,1]=%d,%d) returns:",a[0],a[1]);
  155. Xprintf("%d",FREV(a)); printf(" with a[0,1]=%d,%d\n",a[0],a[1]);
  156. X}
  157. X#endif
  158. X
  159. X#ifdef FCB_SELECT
  160. XPROTOCCALLSFSUB0(ffcb)
  161. X#define FFCB()                 CCALLSFSUB0(ffcb)
  162. X
  163. Xtypedef struct { char v[13],w[4][13],x[2][3][13]; } FCB_DEF;
  164. X#define FCB COMMON_BLOCK(fcb)
  165. Xextern FCB_DEF FCB;
  166. X
  167. Xmain() {
  168. Xchar cv[14];
  169. Xstatic char cw[4][14]    = {"C's w[0]", "C's w[1]", "C's w[2]", "C's w[3]"};
  170. Xstatic char cx[2][3][14] = {"C's x[0][0]", "C's x[0][1]", "C's x[0][2]",
  171. X                            "C's x[1][0]", "C's x[1][1]", "C's x[1][2]"};
  172. XC2FCBSTR("C's V" ,FCB.v,0);
  173. XC2FCBSTR(cw      ,FCB.w,1);
  174. XC2FCBSTR(cx      ,FCB.x,2);
  175. XFFCB();
  176. XFCB2CSTR(FCB.v   ,cv   ,0);
  177. XFCB2CSTR(FCB.w   ,cw   ,1);
  178. XFCB2CSTR(FCB.x   ,cx   ,2);
  179. Xprintf("FCB returns v = %s.\n",cv);
  180. Xprintf("FCB returns w[1,2,3,4] = %s,%s,%s,%s.\n",cw[0],cw[1],cw[2],cw[3]);
  181. Xprintf("FCB returns x[0,(1,2,3)] = %s,%s,%s.\n",cx[0][0],cx[0][1],cx[0][2]);
  182. Xprintf("FCB returns x[1,(1,2,3)] = %s,%s,%s.\n",cx[1][0],cx[1][1],cx[1][2]);
  183. X}
  184. X#endif
  185. X
  186. X#ifdef EQ_SELECT
  187. XPROTOCCALLSFSUB0(feq)
  188. X#define FEQ()                 CCALLSFSUB0(feq)
  189. X
  190. X#define KWBANK 690
  191. Xtypedef struct {
  192. X  int nzebra; float gversn,zversn; int ixstor,ixdiv,ixcons; float fendq[16];
  193. X  union {
  194. X    struct {
  195. X      int Lmain,Lr1;
  196. X      union {float Ws[KWBANK]; int Iws[2];}u;
  197. X    }s;
  198. X    union {
  199. X      int Lq[80];
  200. X      struct {
  201. X        int dummy[8];
  202. X        union {float Q[2]; int Iq[2];}u;
  203. X      }s;
  204. X    }u;
  205. X  }u;
  206. X} GCBANK_DEF;
  207. X#define lmain u.s.Lmain
  208. X#define lr1   u.s.Lr1
  209. X#define ws    u.s.u.Ws
  210. X#define iws   u.s.u.Iws
  211. X#define lq    u.u.Lq
  212. X#define q     u.u.s.u.Q
  213. X#define iq    u.u.s.u.Iq
  214. X#define GCBANK COMMON_BLOCK(gcbank)
  215. XGCBANK_DEF GCBANK;
  216. X
  217. Xmain() {
  218. XFEQ();
  219. Xprintf("GCBANK.nzebra       = %d.\n", GCBANK.nzebra);
  220. Xprintf("GCBANK.gversn       = %f.\n", GCBANK.gversn);
  221. Xprintf("GCBANK.zversn       = %f.\n", GCBANK.zversn);
  222. Xprintf("GCBANK.ixstor       = %d.\n", GCBANK.ixstor);
  223. Xprintf("GCBANK.ixcons       = %d.\n", GCBANK.ixcons);
  224. Xprintf("GCBANK.fendq[15]    = %f.\n", GCBANK.fendq[15]);
  225. Xprintf("GCBANK.lmain        = %d.\n", GCBANK.lmain);
  226. Xprintf("GCBANK.lr1          = %d.\n", GCBANK.lr1);
  227. Xprintf("GCBANK.ws[KWBANK-1] = %f.\n", GCBANK.ws[KWBANK-1]);
  228. Xprintf("GCBANK.iq[0]        = %d.\n", GCBANK.iq[0]);
  229. X}
  230. X#endif
  231. X
  232. X/* The following functions are called by FORTRAN functions, as shown by the
  233. X   remaining examples.
  234. X   We redefine the C name of each routine called from FORTRAN so that the
  235. X   FORTRAN call may use the C name, e.g. the next line. */
  236. X#define EXIST ccallsc(exist)
  237. Xvoid EXIST() {printf("EXIST: was called.\n");}
  238. XFCALLSCSUB0(exist)
  239. X
  240. X#define CA ccallsc(ca)
  241. Xvoid CA(int i) {printf("CA: had integer argument:%d.\n",i);}
  242. XFCALLSCSUB1(ca,INT)
  243. X
  244. X#define CB ccallsc(cb)
  245. Xvoid CB(int *i) {
  246. Xprintf("CB: had pointer argument to integer:%d.\n",*i); *i*=2;}
  247. XFCALLSCSUB1(cb,PINT)
  248. X
  249. X#define CC ccallsc(cc)
  250. Xvoid CC(char *s) {printf("CC: had string argument:%s.\n",s);}
  251. XFCALLSCSUB1(cc,STRING)
  252. X
  253. X#define CD ccallsc(cd)
  254. Xvoid CD(char *s)
  255. X{printf("CD: had string argument:%s.\n",s); strcpy(s,"to you 12345678");}
  256. XFCALLSCSUB1(cd,PSTRING)
  257. X
  258. X#define CE ccallsc(ce)
  259. Xvoid CE(char v[][5])
  260. X{printf("CE: had string vector argument:%s,%s,%s.\n",v[0],v[1],v[2]);}
  261. X#define ce_STRV_A1 TERM_CHARS(' ',1)
  262. XFCALLSCSUB1(ce,STRINGV)
  263. X
  264. X#define CF ccallsc(cf)
  265. Xvoid CF(char v[][5], int n)
  266. X{int i;
  267. Xprintf("CF: had %d string vector argument:",n);
  268. Xfor (i=0; i<n-1; i++) printf("%s,",v[i]);
  269. Xprintf("%s.\n",v[i]);
  270. X}
  271. X#define cf_STRV_A1 NUM_ELEM_ARG(2)
  272. XFCALLSCSUB2(cf,STRINGV,INT)
  273. X
  274. X
  275. X#define CG ccallsc(cg)
  276. Xint CG() {return 1;}
  277. XFCALLSCFUN0(INT,cg)
  278. X
  279. X#define CH ccallsc(ch)
  280. Xchar *CH() {return "hello";}
  281. XFCALLSCFUN0(STRING,ch)
  282. X
  283. X#define CI ccallsc(ci)
  284. Xchar *CI(char v[][5]) {return v[3];}
  285. X#define ci_STRV_A1 NUM_ELEMS(6)
  286. XFCALLSCFUN1(STRING,ci,STRINGV)
  287. X
  288. X#define CJ ccallsc(cj)
  289. Xchar *CJ(int v) {printf("CJ:v=%d\n",v);return "hello";}
  290. XFCALLSCFUN1(STRING,cj,INT)
  291. X
  292. X#ifdef F0_SELECT
  293. XPROTOCCALLSFSUB0(fexist)
  294. X#define FEXIST()               CCALLSFSUB0(fexist)
  295. X
  296. Xmain() {FEXIST();}
  297. X#endif
  298. X
  299. X#ifdef FA_SELECT
  300. XPROTOCCALLSFSUB1(fa,INT)
  301. X#define FA(A1)               CCALLSFSUB1(fa,INT,A1)
  302. X
  303. Xmain() {FA(1234);}
  304. X#endif
  305. X
  306. X#ifdef FB_SELECT
  307. XPROTOCCALLSFSUB1(fb,PINT)
  308. X#define FB(A1)               CCALLSFSUB1(fb,PINT,A1)
  309. X
  310. Xmain()
  311. X{int i,ii; i=ii=1234;
  312. X FB(ii); printf("MAIN: FB(i=%d) returns with i=%d.\n",i,ii);}
  313. X#endif
  314. X
  315. X#ifdef FC_SELECT
  316. XPROTOCCALLSFSUB1(fc,STRING)
  317. X#define FC(A1)               CCALLSFSUB1(fc,STRING,A1)
  318. X
  319. Xmain() {FC("hello");}
  320. X#endif
  321. X
  322. X#ifdef FD_SELECT
  323. XPROTOCCALLSFSUB1(fd,PSTRING)
  324. X#define FD(A1)               CCALLSFSUB1(fd,PSTRING,A1)
  325. X
  326. Xmain()
  327. X{static char i[] = "happy     "; static char ii[] = "happy      ";
  328. X FD(ii); printf("MAIN: FD(i=%s) returns with i=%s.\n",i,ii);}
  329. X#endif
  330. X
  331. X#ifdef FE_SELECT
  332. XPROTOCCALLSFSUB1(fe,STRINGV)
  333. X#define FE(A1)               CCALLSFSUB1(fe,STRINGV,A1)
  334. X
  335. Xmain()
  336. X{static char v[][5] = {"0000", "1", "22", ""}; FE(v);}
  337. X#endif
  338. X
  339. X#ifdef FF_SELECT
  340. XPROTOCCALLSFSUB2(ff,STRINGV,INT)
  341. X#define FF(A1,A2)               CCALLSFSUB2(ff,STRINGV,INT, A1,A2)
  342. X
  343. Xmain()
  344. X{static char v[][5] = {"0000", "1", "22", ""};
  345. X FF(v,sizeof(v)/sizeof v[0]);}
  346. X#endif
  347. X
  348. X#ifdef FG_SELECT
  349. XPROTOCCALLSFFUN0(INT,fg)
  350. X#define FG()               CCALLSFFUN0(fg)
  351. X
  352. Xmain()
  353. X{printf("FG() returns %d.\n",FG());}
  354. X#endif
  355. X
  356. X#ifdef FH_SELECT
  357. XPROTOCCALLSFFUN0(STRING,fh)
  358. X#define FH()               CCALLSFFUN0(fh)
  359. X
  360. Xmain()
  361. X{printf("FH() returns %s.\n",FH());}
  362. X#endif
  363. X
  364. X#ifdef FI_SELECT
  365. XPROTOCCALLSFFUN1(STRING,fi,STRINGV)
  366. X#define FI(A1)               CCALLSFFUN1(fi,STRINGV,A1)
  367. X
  368. Xmain()
  369. X{static char v[][5] = {"0000", "1", "22", "333", "8", "9"};
  370. X printf("FI(v) returns %s.\n",FI(v));}
  371. X#endif
  372. X
  373. X#ifdef FJ_SELECT
  374. XPROTOCCALLSFFUN1(STRING,fj,INT)
  375. X#define FJ(A1)               CCALLSFFUN1(fj,INT,A1)
  376. X
  377. Xmain()
  378. X{ printf("FJ(2) returns %s.\n",FJ(2));}
  379. X#endif
  380. END_OF_FILE
  381.   if test 8968 -ne `wc -c <'cfortest.c'`; then
  382.     echo shar: \"'cfortest.c'\" unpacked with wrong size!
  383.   fi
  384.   # end of 'cfortest.c'
  385. fi
  386. if test -f 'cfortex.for' -a "${1}" != "-c" ; then 
  387.   echo shar: Will not clobber existing file \"'cfortex.for'\"
  388. else
  389.   echo shar: Extracting \"'cfortex.for'\" \(4434 characters\)
  390.   sed "s/^X//" >'cfortex.for' <<'END_OF_FILE'
  391. XC cfortex.f
  392. XC Burkhard Burow, University of Toronto, July 1990.
  393. X
  394. X      subroutine s1(b)
  395. X      character*(*) b
  396. X      character*(13) a
  397. X      data a/'first'/
  398. X      b = a
  399. X      return
  400. X      end
  401. X
  402. X      subroutine abc(a,b,c)
  403. X      character*(*) b,a,c
  404. X      character*(13) d
  405. X      d = a
  406. X      a = b
  407. X      b = c
  408. X      c = d
  409. X      return
  410. X      end
  411. X
  412. X      subroutine forstr1(b)
  413. X      character*(*) b
  414. X      character*(13) a
  415. X      character*(13) forstr
  416. X      data a/'firs'/
  417. X      b = forstr(a)
  418. X      return
  419. X      end
  420. X
  421. X
  422. X      subroutine EASY(a,b)
  423. X      a = b
  424. X      return
  425. X      end
  426. X
  427. X      character*(*) function forstr(a)
  428. X      character*(*) a
  429. X      forstr = a
  430. X      return
  431. X      end
  432. X
  433. X      function r(i)
  434. X      r = i
  435. X      return
  436. X      end
  437. X
  438. X      character*(*) function forstr2()
  439. X      character*(13) a
  440. X      data a/'first'/
  441. X      forstr2 = a
  442. X      return
  443. X      end
  444. X
  445. X      character*(*) function ft(v, w, a)
  446. X      character *(*) v(4), w(4)
  447. X      print*,'FT:len(v(1 or 2 or 3 or 4))  =',len(v(1))
  448. X      print*,'FT:len(w(1 or 2 or 3))    =',len(w(1))
  449. X      print*,'FT:a = ',a
  450. X      print*,'FT:v(1,2,3,4) =',v(1),',',v(2),',',v(3),',',v(4)
  451. X      print*,'FT:w(1,2,3,4) =',w(1),',',w(2),',',w(3),',',w(4)
  452. X      ft = v(1)
  453. X      return
  454. X      end
  455. X
  456. X      subroutine st(v, w, a)
  457. X      character *(*) v(4), w(4)
  458. X      print*,'ST:len(v(1 or 2 or 3 or 4))  =',len(v(1))
  459. X      print*,'ST:len(w(1 or 2 or 3))    =',len(w(1))
  460. X      print*,'ST:a = ',a
  461. X      print*,'ST:v(1,2,3,4) =',v(1),',',v(2),',',v(3),',',v(4)
  462. X      print*,'ST:w(1,2,3,4) =',w(1),',',w(2),',',w(3),',',w(4)
  463. X      return
  464. X      end
  465. X
  466. X      subroutine rev(a)
  467. X      integer a(2),t
  468. X      t    = a(1)
  469. X      a(1) = a(2)
  470. X      a(2) = t
  471. X      return
  472. X      end
  473. X
  474. X      integer function frev(a)
  475. X      integer a(2)
  476. X      frev = a(1)
  477. X      a(1) = a(2)
  478. X      a(2) = frev
  479. X      return
  480. X      end
  481. X
  482. X      subroutine ffcb()
  483. X      common /fcb/  v,w,x
  484. X      character *(13) v, w(4), x(3,2)
  485. X      print*,'FFCB:v =',v,'.'
  486. X      print*,'FFCB:w(1,2,3,4) =',w(1),',',w(2),',',w(3),',',w(4),'.'
  487. X      print*,'FFCB:x([1,2,3],1) =',x(1,1),',',x(2,1),',',x(3,1),'.'
  488. X      print*,'FFCB:x([1,2,3],2) =',x(1,2),',',x(2,2),',',x(3,2),'.'
  489. X      v      = 'fcb v'
  490. X      w(1)   = 'fcb w(1)'
  491. X      w(2)   = 'fcb w(2)'
  492. X      w(3)   = 'fcb w(3)'
  493. X      x(1,1) = 'fcb x(1,1)'
  494. X      x(2,1) = 'fcb x(2,1)'
  495. X      x(3,1) = 'fcb x(3,1)'
  496. X      x(1,2) = 'fcb x(1,2)'
  497. X      x(2,2) = 'fcb x(2,2)'
  498. X      x(3,2) = 'fcb x(3,2)'
  499. X      end
  500. X
  501. X      subroutine feq()
  502. X      parameter (kwbank=690)
  503. X      common/gcbank/nzebra,gversn,zversn,ixstor,ixdiv,ixcons,fendq(16)
  504. X     +             ,lmain,lr1,ws(kwbank)
  505. X      dimension iq(2),q(2),lq(80),iws(2)
  506. X      equivalence (q(1),iq(1),lq(9)),(lq(1),lmain) ,(iws(1),ws(1))
  507. X      nzebra     = 1
  508. X      gversn     = 2
  509. X      zversn     = 3
  510. X      ixstor     = 4
  511. X      ixcons     = 5
  512. X      fendq(16)  = 6
  513. X      lmain      = 7
  514. X      lr1        = 8
  515. X      ws(kwbank) = 9
  516. X      lq(9)      = 10
  517. X      end
  518. X
  519. X      subroutine fexist()
  520. X      print*,'FEXIST: was called'
  521. X      call exist()
  522. X      return
  523. X      end
  524. X
  525. X      subroutine fa(i)
  526. X      integer i
  527. X      print*,'FA: integer argument =',i
  528. X      call ca(i)
  529. X      return
  530. X      end
  531. X
  532. X      subroutine fb(i)
  533. X      integer i
  534. X      print*,'FB: integer argument =',i
  535. X      i = i*2
  536. X      call cb(i)
  537. X      return
  538. X      end
  539. X
  540. X      subroutine fc(b)
  541. X      character*(*) b
  542. X      print*,'FC: string argument =',b
  543. X      call cc(b)
  544. X      return
  545. X      end
  546. X
  547. X      subroutine fd(b)
  548. X      character*(*) b
  549. X      character*(13) a
  550. X      data a/'birthday'/
  551. X      b = a
  552. X      call cd(b)
  553. X      return
  554. X      end
  555. X
  556. X      subroutine fe(v)
  557. X      character*(*) v(4)
  558. X      print*,'FE:len(v(1 or 2 or 3 or 4))  =',len(v(1))
  559. X      print*,'FE:v(1,2,3,4) =',v(1),',',v(2),',',v(3),',',v(4)
  560. X      call ce(v)
  561. X      return
  562. X      end
  563. X
  564. X      subroutine ff(v,n)
  565. X      character*(*) v(4)
  566. X      print*,'FF:len(v(1 or 2 or 3 or 4))  =',len(v(1))
  567. X      print*,'FF:v(1,2,3,4) =',v(1),',',v(2),',',v(3),',',v(4)
  568. X      print*,'FF:n =',n
  569. X      call cf(v,n)
  570. X      return
  571. X      end
  572. X
  573. X      integer function fg()
  574. X      integer cg
  575. X      fg = cg()
  576. X      return
  577. X      end
  578. X
  579. X      character*(*) function fh()
  580. X      character*200 ch
  581. X      fh = ch()
  582. X      return
  583. X      end
  584. X
  585. X      character*(*) function fi(v)
  586. X      character*(*) v(6)
  587. X      character*200 ci
  588. X      fi = ci(v)
  589. X      return
  590. X      end
  591. X
  592. X      character*(*) function fj(v)
  593. X      integer v
  594. X      character*200 cj
  595. X      print*,'FJ:v =',v
  596. X      fj = cj(v)
  597. X      return
  598. X      end
  599. X
  600. END_OF_FILE
  601.   if test 4434 -ne `wc -c <'cfortex.for'`; then
  602.     echo shar: \"'cfortex.for'\" unpacked with wrong size!
  603.   fi
  604.   # end of 'cfortex.for'
  605. fi
  606. if test -f 'cfortran.doc' -a "${1}" != "-c" ; then 
  607.   echo shar: Will not clobber existing file \"'cfortran.doc'\"
  608. else
  609.   echo shar: Extracting \"'cfortran.doc'\" \(27512 characters\)
  610.   sed "s/^X//" >'cfortran.doc' <<'END_OF_FILE'
  611. X/* cfortran.doc */
  612. X/* Burkhard Burow, burow@vxdesy.desy.de, U. of Toronto, 1991. */
  613. X
  614. X
  615. X                CFORTRAN 1.2 for UNIX Machines and for VAX VMS
  616. X
  617. XHistory:
  618. X- 1.0 for VAX VMS using C 3.1 and FORTRAN 5.4.                        Oct. '90.
  619. X- 1.0 for Silicon Graphics using Mips Computer System 2.0 f77 and cc. Feb. '91.
  620. X          [Port of C calls FORTRAN half only.]
  621. X- 1.1 for Mips Computer System 2.0 f77 and cc.                        Mar. '91.
  622. X          [Runs on at least: Silicon Graphics IRIX 3.3.1
  623. X                             DECstations with Ultrix V4.1]
  624. X- 1.2 Internals are simpler, smaller, faster, stronger.               May  '91.
  625. X      Mips version works on IBM RS/6000, this is now called the unix version.
  626. X- 1.3 UNIX and VAX VMS versions are merged into a single cfortran.h.  June '91.
  627. X      C can manipulate (arrays of) strings in FORTRAN common blocks.
  628. X
  629. X
  630. XI Introduction
  631. X--------------
  632. X
  633. XCFORTRAN is an easy-to-use powerful bridge between C and FORTRAN. It provides a
  634. Xcompletely transparent, machine independant, interface  between C and FORTRAN
  635. Xroutines (= subroutines and/or functions).
  636. X
  637. XThe complete CFORTRAN package consists of 4 files. They are this introduction,
  638. Xcfortran.doc, the engine in cfortran.h, examples in cfortest.c and
  639. Xcfortex.f/or. [cfortex.for under VMS, cfortex.f under UNIX.]
  640. X
  641. XTo run the example do the following:
  642. X
  643. XRS/6000> cc -Drs6000 -c cfortest.c && xlf -o cfortest cfortest.o cfortex.f
  644. XRS/6000> cfortest
  645. X
  646. Xor
  647. X
  648. XMIPS> cc -o cfortest cfortest.c cfortex.f -lI77 -lU77 -lF77
  649. XMIPS> cfortest
  650. X
  651. Xor
  652. X
  653. XVMS> define lnk$library sys$library:vaxcrtl
  654. XVMS> cc cfortest.c
  655. XVMS> fortran cfortex.for
  656. XVMS> link/exec=cfortest cfortest,cfortex
  657. XVMS> run cfortest
  658. X
  659. XBy changing the SELECTion ifdef of cfortest.c and recompiling you can try out
  660. Xa dozen different few-line examples.
  661. X
  662. X
  663. X
  664. XThe benefits of using CFORTRAN include:
  665. X1. Machine independant applications.
  666. X
  667. X2. Identical (within syntax) calls across languages, e.g.
  668. XC FORTRAN
  669. X      CALL HBOOK1(1,'pT spectrum of pi+',100,0.,5.,0.)
  670. X/* C*/
  671. X           HBOOK1(1,"pT spectrum of pi+",100,0.,5.,0.);
  672. X
  673. X3. Each routine need ony be set up once in its lifetime. e.g.
  674. X/* Setting up a FORTRAN routine to be called by C. Note that ID,...,VMX are
  675. Xmerely the names of arguments. These tags must be unique w.r.t. each other but
  676. Xare arbitrary. */
  677. XPROTOCCALLSFSUB6(hbook1,INT,STRING,INT,FLOAT,FLOAT,FLOAT)
  678. X#define HBOOK1(ID,CHTITLE,NX,XMI,XMA,VMX)                 \
  679. X     CCALLSFSUB6(hbook1,INT,STRING,INT,FLOAT,FLOAT,FLOAT, \
  680. X               ID,CHTITLE,NX,XMI,XMA,VMX)
  681. X
  682. X4. Routines, and the code calling them, can be coded naturally in the language
  683. X   of choice. C routines may be coded with the natural assumption that they'll
  684. X   only be called by C code. CFORTRAN does all the required work for FORTRAN
  685. X   code to call C routines. Similarly CFORTRAN does all the work required for C
  686. X   to call FORTRAN routines. Therefore:
  687. X     - C programmers need not imbed FORTRAN argument passing mechanisms into
  688. X       their code.
  689. X     - FORTRAN code need not be converted into C code. i.e. The honed and
  690. X       timehonored FORTRAN routines are called by C, not some new translation
  691. X       from FORTRAN into C.
  692. X
  693. X5. CFORTRAN is contained within a single C include file, cfortran.h, weighing
  694. X   in at ~1100 lines. cfortran.h currently supports VAX VMS, the IBM RS/6000
  695. X   and machines using the MIPS RISC compilers. It should be portable to many
  696. X   other platforms.
  697. X
  698. X6. STRINGS and VECTORS of STRINGS along with the usual simple arguments to
  699. X   routines are supported as are functions returning STRINGS or numbers.
  700. X
  701. X7. CFORTRAN requires each routine to be exported to be explicitly set up. While
  702. X   this need usually only be done once in a header file it would be best if
  703. X   applications were required to do no work at all in order to cross languages.
  704. X   CFORTRAN's simple syntax could be a convinient back-end for a program which
  705. X   would export FORTRAN or C routines directly from the source code.
  706. X
  707. X
  708. X                                    -----
  709. X
  710. XExample 1 - CFORTRAN has been used to make the C header file hbook.h,
  711. X            which then gives any C programmer, e.g. example.c, full and
  712. X            completely transparent access to CERN's HBOOK library of routines.
  713. X            Each HBOOK routine required about 3 lines of simple code in
  714. X            hbook.h. The example also demonstrates how FORTRAN common blocks
  715. X            are defined and used.
  716. X
  717. X/* hbook.h */
  718. X#include "cfortran.h"
  719. X        :
  720. XPROTOCCALLSFSUB6(hbook1,INT,STRING,INT,FLOAT,FLOAT,FLOAT)
  721. X#define HBOOK1(ID,CHTITLE,NX,XMI,XMA,VMX) \
  722. X     CCALLSFSUB6(hbook1,INT,STRING,INT,FLOAT,FLOAT,FLOAT, \
  723. X               ID,CHTITLE,NX,XMI,XMA,VMX)
  724. X        :
  725. X/* end hbook.h */
  726. X
  727. X/* example.c */
  728. X#include "hbook.h"
  729. X        :
  730. Xtypedef struct {
  731. X  int lines;
  732. X  int status[SIZE];
  733. X  float p[SIZE];  /* momentum */
  734. X} FAKE_DEF;
  735. X#define FAKE COMMON_BLOCK(fake)
  736. Xextern FAKE_DEF FAKE;
  737. X        :
  738. Xmain ()
  739. X{
  740. X        :
  741. X           HBOOK1(1,"pT spectrum of pi+",100,0.,5.,0.);
  742. X/* c.f. the call in FORTRAN:
  743. X      CALL HBOOK1(1,'pT spectrum of pi+',100,0.,5.,0.)
  744. X*/
  745. X        :
  746. X  FAKE.p[7]=1.0;
  747. X        :
  748. X}
  749. X
  750. XN.B. i) The routine is language independant.
  751. X    ii) hbook.h is machine independant.
  752. X   iii) Applications using CFORTRAN'd routines are machine independant.
  753. X
  754. X                                    -----
  755. X
  756. XExample 2 - Many VMS System calls are most easily called from FORTRAN, but
  757. X            CFORTRAN now gives you that ease in C.
  758. X
  759. X#include "cfortran.h"
  760. X
  761. XPROTOCCALLSFSUB3(lib$spawn, STRING,STRING,STRING)
  762. X#define LIB$SPAWN(command,input_file,output_file) \
  763. X     CCALLSFSUB3(lib$spawn, STRING,STRING,STRING, \
  764. X                  command,input_file,output_file)
  765. X
  766. Xmain ()
  767. X{
  768. XLIB$SPAWN("set term/width=132","","");
  769. X}
  770. X
  771. XObviously the 2 CFORTRAN lines above should be put into a header file along
  772. Xwith the description of the other system calls, but as this example shows it's
  773. Xnot much hassle to set up CFORTRAN for even a single call.
  774. X
  775. X                                    -----
  776. X
  777. XExample 3 - CFORTRAN and the source cstring.c create the cstring.obj library
  778. X            which gives FORTRAN access to all the functions in C's system
  779. X            library described by the system's C header file string.h.
  780. X
  781. XC     EXAMPLE.FOR
  782. X      PROGRAM EXAMPLE
  783. X      DIMENSION I(20), J(30)
  784. X        :
  785. X      CMEMCPY(I,J,7)
  786. X        :
  787. X      END
  788. X
  789. X/* cstring.c */
  790. X#include <string.h>
  791. X#include "cfortran.h"
  792. X
  793. X#undef fcallsc
  794. X#define fcallsc(NAME) C/**/NAME
  795. X
  796. X        :
  797. XFCALLSCSUB3(memcpy, PVOID, PVOID, INT)
  798. X        :
  799. X
  800. XN.B. Other than a possible redefinition of fcallsc, cstring.c is machine
  801. Xindependant. Unfortunately the names of C routines called by FORTRAN may
  802. Xdiffer from  the name of the original C routine, e.g. cmemcpy vs. the original
  803. Xmemcpy. This need never be the case if one has:
  804. X
  805. X   i) the original C source code for the routine.
  806. XOR ii)a FORTRAN compiler, e.g. f77, which 'renames' routines.
  807. XOR iii) a case sensitive linker.
  808. X
  809. XIf all the above fail, CFORTRAN, through fcallsc, makes it easy to ensure that
  810. Xnames of C routines called by FORTRAN are modified from  the original only when
  811. Xabsolutely neccessary, and if they are modified, that it is done consistently
  812. Xfor any given FORTRAN compiler. [More details below in Section VI.]
  813. X
  814. X                                    -----
  815. X
  816. X
  817. XII Using CFORTRAN
  818. X-----------------
  819. X
  820. XThe user is asked to look at the source files CFORTEX.C and CFORTEX.FOR for
  821. Xclarification by example.
  822. X
  823. XNote: CFORTRAN (ab)uses the null comment, /**/, kludge for the ANSI C
  824. Xpreprocessor concatenation, ##, operator. In MIPS C this kludge is sensitive
  825. Xto blanks prepending arguments to macros.
  826. XTHEREFORE IN THE FOLLOWING MACRO DEFINITIONS YOU MAY NOT PREPEND argtype_i NOR
  827. Xroutine_type WITH BLANK, ' ', CHARACTERS.
  828. X
  829. XNote: On the RS/6000, currently the only machine supporting ##, a global
  830. Xreplace of /**/ by ## makes cfortran.h ANSI compliant.
  831. X
  832. XNote: On the RS/6000, if you use "xlf -qextname ...", which appends an
  833. Xunderscore, '_', to all FORTRAN external references, then you must use,
  834. X"cc -Dextname ..." so that cfortran.h also generates these underscores.
  835. X
  836. XNote: The calls to FORTRAN subroutines are expanded to code inside {....}. This
  837. Xhas some undesirable side effects, e.g.
  838. Xif (a==b) A_FORTRAN_SUBROUTINE(1,"Hello")  /* <-- No ';' allowed here. */
  839. Xelse      A_FORTRAN_SUBROUTINE(1,"Hello")
  840. XUnfortunately there is no fix in sight. (gcc extensions to C would offer a fix.)
  841. X
  842. XNote: At the moment only vectors of fixed length strings are supported in C. I
  843. Xknow how and hope to support vectors of pointers to strings in the near future.
  844. X
  845. XNote: For those who wish to use CFORTRAN in large applications.
  846. XThis release is intended to make it easy to get applications up and running.
  847. XThis implies that applications are not as efficient as they could be:
  848. X- The current mechanism is inefficient if a single header file is used to
  849. X  describe a large library of FORTRAN functions. Code for a static wrapper fn.
  850. X  is generated in each piece of C source code for each FORTRAN function
  851. X  specified with the CCALLSFFUNn statement, irrespective of whether or not the
  852. X  function is ever called. I have several ideas for how code for these wrappers
  853. X  could be created, compiled and linked only once instead of once for each
  854. X  piece of source code.
  855. X- Code for several static utility routines internal to CFORTRAN is placed into
  856. X  any source code which #include's cfortran.h. These routines should be in a
  857. X  library.
  858. X- The FORTRAN calls C half of the package could be split from the C calls
  859. X  FORTRAN half.
  860. X
  861. X
  862. Xi) Calling FORTRAN routines from C:
  863. X   --------------------------------
  864. X
  865. XFORTRAN routines are prototyped by the following two macros.
  866. X
  867. XPROTOCCALLSFSUBn(routine_name, argtype_1, ..., argtype_n)
  868. X
  869. Xor
  870. X
  871. XPROTOCCALLSFFUNn(routine_type, routine_name, argtype_1, ..., argtype_n)
  872. X
  873. X
  874. Xand are defined respectively by the following two macro usages.
  875. X
  876. X#define      ROUTINE_NAME(argname_1,...,argname_n) \
  877. XCCALLSFSUBn(routine_name, argtype_1,...,argtype_n, \
  878. X                          argname_1,...,argname_n)
  879. X
  880. X#define      ROUTINE_NAME(argname_1,...,argname_n) \
  881. XCCALLSFFUNn(routine_name, argtype_1,...,argtype_n, \
  882. X                          argname_1,...,argname_n)
  883. X
  884. XWhere:
  885. X'n' = 0->7 (easily expanded in CFORTRAN.H to >7) stands for the number of
  886. X    arguments to the routine.
  887. XROUTINE_NAME = the C       name of the routine (IN UPPERCASE LETTERS).
  888. Xroutine_name = the FORTRAN name of the routine (IN lowercase LETTERS).
  889. Xroutine_type = the type of argument returned by FORTRAN functions.
  890. X             = DOUBLE, FLOAT, INT, LOGICAL, LONG, STRING.
  891. Xargtype_i    = the type of argument passed to the FORTRAN routine and must be
  892. X               consistent in the definition and prototyping of the routine s.a.
  893. X             = DOUBLE, FLOAT, INT, LOGICAL, LONG, STRING.
  894. X             For vectors, i.e. 1 dim. arrays use
  895. X             = DOUBLEV, FLOATV, INTV, LOGICALV, LONGV.
  896. X             For vectors of vectors, 2 dim. arrays use
  897. X             = DOUBLEVV, FLOATVV, INTVV, LOGICALVV, LONGVV, STRINGV.
  898. X             For n-dim. arrays use
  899. X             = DOUBLEV..nV's..V, FLOATV..V, INTV..V, LOGICALV..V, LONGV..V.
  900. X                N.B. Array dimensions and types are checked by the C compiler.
  901. X             For routines changing the values of an argument, the keyword is
  902. X                  prepended by a 'P'.
  903. X             = PDOUBLE, PFLOAT, PINT, PLOGICAL, PLONG, PSTRING, PSTRINGV.
  904. X             For exceptional arguments which require no massaging to fit the
  905. X                  argument passing mechanisms use:
  906. X             = PVOID.
  907. X                This is most useful for passing functions as arguments.
  908. X                But note that although PVOID could be used to describe all
  909. X                array arguments on most (all?) machines , it shouldn't be
  910. X                because the C compiler can no longer check the type and
  911. X                dimension of the array.
  912. Xargname_i    = any valid unique C tag, but must be consistent in the definition
  913. X               as shown.
  914. X
  915. XSome notes on (P)STRING(V):
  916. X
  917. XSTRING - If the argument is a fixed length character array, e.g. char ar[8];,
  918. Xthe string is blank, ' ', padded on the right to fill out the array before
  919. Xbeing passed to the FORTRAN routine. The useful size of the string is the same
  920. Xin both languages, e.g. we pass ar[8] as character*7. If the argument is a
  921. Xpointer, we cannot blank pad, and pass the length as strlen(argument). On
  922. Xreturn from the FORTRAN routine, pointer arguments are not disturbed, arrays
  923. Xhave the terminating '\0' replaced to its original position. i.e. The
  924. Xpadding blanks are never visible to the C code.
  925. X
  926. XPSTRING - The argument is massaged as with STRING before being passed to the
  927. XFORTRAN routine. On return, the argument has all trailing blanks removed,
  928. Xregardless of whether the argument was a pointer or an array.
  929. X
  930. XN.B. Only char arrays are supported for (P)STRINGV. e.g. char bb[6][8];
  931. X
  932. XSTRINGV - The elements of the argument are copied into space malloc'd, and each
  933. Xelement is padded with blanks. The useful size of each element is the same in
  934. Xboth languages. Therefore char bb[6][8]; is equivalent to character*7 bb(6).
  935. XOn return from the routine the malloc'd space is simply released.
  936. X
  937. XPSTRINGV - Since FORTRAN has no trailing '\0', elements in an array of strings
  938. Xare contiguous. Therefore we pad each element of the C array with blanks and
  939. Xstrip out C's trailing '\0'. After returning from the routine, we reinsert the
  940. Xtrailing '\0' and kill the trailing blanks in each element.
  941. X
  942. XSummary: STRING(V) arguments are blank padded during the call to the FORTRAN
  943. Xroutine, but remain original in the C code. (P)STRINGV arguments are blank
  944. Xpadded for the FORTRAN call, and after returning from FORTRAN trailing blanks
  945. Xare stripped off.
  946. X
  947. X
  948. XPVOID, as noted above, is used to declare that a function will be passed as an
  949. Xargument. In order to perform the call, CFORTRAN must know the language of the
  950. Xfunction to be passed, therefore the when passing C functions to FORTRAN
  951. Xroutines use:
  952. X
  953. X    FORTRAN_ROUTINE( ...., C_FUNCTION(some_function), ...)
  954. X
  955. Xand similarly when passing a FORTRAN routine:
  956. X
  957. X    FORTRAN_ROUTINE( ...., FORTRAN_FUNCTION(some_function), ...)
  958. X
  959. X
  960. XThis list of argument types is not neccessarily complete. CFORTRAN may be
  961. Xexpanded to handle a new type not among the above.
  962. X
  963. XN.B. The FORTRAN routines are called using macro expansions, therefore the
  964. Xusual caveats for expressions in arguments apply. The expressions to the
  965. Xroutines may be evaluated more than once, leading to lower performance and in
  966. Xthe worst case bizzare bugs.
  967. X
  968. X
  969. Xii) Calling C routines from FORTRAN:
  970. X    --------------------------------
  971. X
  972. XNote that each of the following two statements to export a C routine to FORTRAN
  973. Xcreate FORTRAN 'wrappers', written in C, which must be compiled and linked
  974. Xalong with the original C routines and with the FORTRAN calling code.
  975. X
  976. XVAX VMS user's will have to redefine the one of the macros fcallsc or ccallsc.
  977. XSee the examples or existing applications for details and information.
  978. X
  979. XFCALLSCSUBn(routine_name, argtype_1, ..., argtype_n)
  980. X
  981. Xor
  982. X
  983. XFCALLSCFUNn(routine_type, routine_name, argtype_1, ..., argtype_n)
  984. X
  985. XWhere:
  986. X'n' = 0->7 (easily expanded to >7) stands for the number of arguments to the
  987. X    routine.
  988. Xroutine_name = the FORTRAN name of the routine (IN lowercase LETTERS).
  989. Xroutine_type = the type of argument returned by C functions.
  990. X             = DOUBLE, FLOAT, INT, LOGICAL, LONG, STRING.
  991. Xargtype_i    = the type of argument passed to the FORTRAN routine and must be
  992. X               consistent in the definition and prototyping of the routine
  993. X             = DOUBLE, FLOAT, INT, LOGICAL, LONG, STRING, STRINGV.
  994. X               For arrays or for routines changing the values of any of their
  995. X                arguments; the C routines expect pointers to these arguments,
  996. X                so the keywords are prepended by a 'P'.
  997. X             = PDOUBLE, PFLOAT, PINT, PLOGICAL, PLONG, PSTRING, PSTRINGV,
  998. X               PVOID.
  999. X               The keyword PVOID is a generic form of the nonSTRING types.
  1000. X               STRINGV refers to vector of strings.
  1001. X
  1002. X
  1003. X(P)STRING arguments have any trailing blanks removed before being passed to C,
  1004. Xthe same holds true for each element in (P)STRINGV. Space is malloc'd in all
  1005. Xcases big enough to hold the original string (elements) as well as C's
  1006. Xterminatinng '\0'. i.e. The useful size of the string (elements) is the same in
  1007. Xboth languages. PSTRING(V) => the string (elements) will be copied from the
  1008. Xmalloc'd space back into the FORTRAN bytes.
  1009. X
  1010. XTHE FOLLOWING APPLIES TO THE UNIX COMPILERS ONLY:
  1011. X                             ----
  1012. X(P)STRINGV for UNIX only: CFORTRAN cannot convert the FORTRAN vector of STRINGS
  1013. Xto the required C vector of STRINGS without explicitly knowing the number of
  1014. Xelements in the vector. The application must do one of the following for each
  1015. X(P)STRINGV argument in a routine before that routine's FCALLSCFUNn/SUBn is
  1016. Xcalled:
  1017. X
  1018. X#define routine_name_STRV_Ai NUM_ELEMS(j)
  1019. X or
  1020. X#define routine_name_STRV_Ai NUM_ELEM_ARG(k)
  1021. X or
  1022. X#define routine_name_STRV_Ai TERM_CHARS(l,m)
  1023. X
  1024. Xwhere: routine_name     is as above.
  1025. X       i [i=1->n.]      specifies the argument number of a STRING VECTOR.
  1026. X       j                would specify a fixed number of elements.
  1027. X       k [k=1->n. k!=i] would specify an integer argument which specifies the
  1028. X                        number of elements.
  1029. X       l [char]         the terminating character at the beginning of an
  1030. X                        element, indicating to cfortran that the preceeding
  1031. X                        elements in the vector are the valid ones.
  1032. X       m [m=1-...]      the number of terminating characters required to appear
  1033. X                        at the beginning of the terminating string element.
  1034. X                        Note that the terminating element is NOT possed on to
  1035. X                        the C routine.
  1036. X
  1037. Xe.g.
  1038. XCFORTRAN will pass on all elements, in the 1st and only argument to the C
  1039. Xroutine ce, of the STRING VECTOR until, but not including, the first string
  1040. Xelement beginning with 2 blank, ' ', characters.
  1041. X
  1042. X#define ce_STRV_A1 TERM_CHARS(' ',2)
  1043. XFCALLSCSUB1(ce,STRINGV)
  1044. X
  1045. XAgain the lists of types are not neccessarily complete. CFORTRAN may be
  1046. Xexpanded to handle a new type not among the above.
  1047. X
  1048. X
  1049. Xiii) Using C to manipulate FORTRAN COMMON BLOCKS:
  1050. X     -------------------------------------------------------
  1051. X
  1052. XFORTRAN common blocks are set up with the following construct:
  1053. X
  1054. X#define COMMON_BLOCK_NAME COMMON_BLOCK(common_block_name)
  1055. X
  1056. Xwhere common_block_name is given in the case shown. This construct exists to
  1057. Xensure that C code accessing the common block is machine independant.
  1058. X
  1059. X
  1060. XC programs can place a string (or a multidimensional array of strings) into a
  1061. XFORTRAN common block using the following call:
  1062. X
  1063. XC2FCBSTR( CSTR, FSTR, DIMENSIONS);
  1064. X
  1065. Xwhere:
  1066. X
  1067. XCSTR is a pointer to the first element of C's copy of the string (array).
  1068. X     The C code must use a duplicate of, not the original, common block string,
  1069. X     because the FORTRAN common block does not allocate space for C strings'
  1070. X     terminating '\0'.
  1071. X
  1072. XFSTR is a pointer to the first element of the string (array) in the common
  1073. X     block.
  1074. X
  1075. XDIMENSIONS is the number of dimensions of string array.
  1076. X     e.g. char a[10]      has DIMENSIONS=0.
  1077. X          char aa[10][17] has DIMENSIONS=1.
  1078. X          etc...
  1079. X
  1080. XC2FCBSTR will copy the string (array) from CSTR to FSTR, padding with blanks,
  1081. X' ', the trailing characters as required. C2FCBSTR uses DIMENSIONS and FSTR to
  1082. Xdetermine the lengths of the individual string elements and the total number of
  1083. Xelements in the string array.
  1084. X
  1085. XNote that:
  1086. X- the number of string elements in CSTR and FSTR are identical.
  1087. X- for arrays of strings, the useful lengths of strings in CSTR and FSTR must be
  1088. X  the same. i.e. CSTR elements each have 1 extra character to accomodate the
  1089. X  terminating '\0'.
  1090. X
  1091. X
  1092. XFCB2CSTR( FSTR, CSTR, DIMENSIONS)
  1093. X
  1094. Xis the inverse of C2FCBSTR, and shares the same arguments and caveats. Note
  1095. Xthat FCB2CSTR copies each string element of FSTR to CSTR, minus FORTRAN
  1096. Xstrings' trailing blanks.
  1097. X
  1098. X
  1099. XCFORTRAN USERS ARE STRONGLY URGED TO EXAMINE THE COMMON BLOCK EXAMPLES IN
  1100. XCFORTEST.C AND CFORTEX.FOR. The use of strings in common blocks is
  1101. Xdemonstrated, along with a suggested way for C to imitate FORTRAN EQUIVALENCE'd
  1102. Xvariables.
  1103. X
  1104. X
  1105. X              ===> USER'S OF CFORTRAN NEED READ NO FURTHER <===
  1106. X
  1107. X
  1108. XIII Some Details of and Comments on CFORTRAN
  1109. X--------------------------------------------
  1110. X
  1111. XThe following notes should be useful to those wishing to port CFORTRAN to new
  1112. Xtypes of machines.
  1113. X
  1114. X
  1115. XCFORTRAN.H consist of about 1000 lines of source code. Only about 300 lines of
  1116. XCFORTRAN.H are interesting, the rest are slightly modified 'repeats'. Porting
  1117. XCFORTRAN applications, e.g. the hbook.h and cstring.c mentioned above, to
  1118. Xother machines is trivial.  hbook.h is machine independant, and cstring.c will
  1119. Xat most need to have the 'fcallsc' macro redefined. Porting CFORTRAN itself
  1120. Xrequires a solid knowledge of the new machines C preprocessor, and its FORTRAN
  1121. Xargument passing mechanisms. Logically CFORTRAN exists as two halves, a "C
  1122. XCALLS FORTRAN" and a "FORTRAN CALLS C" utility. In some cases it may be
  1123. Xperfectly reasonable to port only 'one half' of CFORTRAN onto a new system.
  1124. X
  1125. X
  1126. XCFORTRAN is simple enough to be used by the most basic of applications, i.e.
  1127. Xmaking a single C/FORTRAN routine available to the FORTRAN/C programmers. Yet
  1128. XCFORTRAN is powerful enough to easily make entire C/FORTRAN libraries available
  1129. Xto FORTRAN/C programmers.
  1130. X
  1131. X
  1132. XCFORTRAN is the ideal tool for FORTRAN libraries which are being rewritten in
  1133. XC. It allows the routines to be written in 'natural C', without having to
  1134. Xconsider the FORTRAN argument passing mechanisms of any machine. It also allows
  1135. XC code accessing these rewritten routines, to use the C entry point. Without
  1136. XCFORTRAN one could fall into the perverse practice of C code calling a C
  1137. Xfunction using FORTRAN argument passing mechanisms!
  1138. X
  1139. X
  1140. XPerhap the philosophy and mechanisms of CFORTRAN could be used and extended
  1141. Xto create other language bridges such as ADAFORTRAN, CPASCAL, COCCAM, etc.
  1142. X
  1143. X
  1144. X
  1145. XIV Pros, Cons and Improvements to CFORTRAN
  1146. X------------------------------------------
  1147. X
  1148. XThe C calls FORTRAN half is all pro. A list would include:
  1149. X
  1150. Xi) Machine independant and C or FORTRAN independant calls to FORTRAN code.
  1151. Xe.g. C      :       hbook1(1,"pT spectrum of pi+",100,0.,5.,0.);
  1152. X     FORTRAN:  call hbook1(1,'pT spectrum of pi+',100,0.,5.,0.)
  1153. Xii) Non-STRING(V) arguments have no, or at most one assignment as overhead.
  1154. Xiii) 'Input only' arguments are protected by using an intermediate value.
  1155. Xiv) I don't think STRING(V)'s can be handled much faster, even in individually
  1156. Xtuned routines.
  1157. X
  1158. X
  1159. XThe FORTRAN calls C half has the fundamental inelegancy of using an
  1160. Xintermediate function. Perhaps a preprocessor and those %DEF (?) FORTRAN
  1161. Xextensions could help. I don't know, I'm just a C programmer who wants to use
  1162. Xroutines written in FORTRAN.
  1163. X
  1164. X
  1165. XIt might make sense to have separate CFORTRAN and FORTRANC utilities, but I've
  1166. Xleft them tied together for the moment.
  1167. X
  1168. X
  1169. XUsing FCALLSCFUNn and CCALLSFFUNn for a function in the same source code, i.e.
  1170. Xcreating the FORTRAN entry to a C function and then allowing C to call this
  1171. XFORTRAN entry, obviously serves only test purposes. Note that the order given
  1172. Xabove is a must, and that a compiler warning is generated because the FORTRAN
  1173. Xfunction prototype generated by CCALLSFFUNn does not match the entry point
  1174. Xcreated by FCALLSCFUNn. This might be fixable, see CFORTRAN.H, but since these
  1175. Xcombo.'s are used in tests only, I don't think it's worth it. I say might
  1176. Xbecause I'm not sure one can satisfy the case sensitive compiler here.
  1177. X
  1178. X
  1179. X
  1180. XV Machine Dependancies of CFORTRAN
  1181. X----------------------------------
  1182. X
  1183. XI leave it to the lucky programmer porting CFORTRAN to a new machine, to
  1184. Xdiscover the FORTRAN argument passing mechanisms. A safe starting point is to
  1185. Xassume that variables and arrays are simply passed by reference as they are in
  1186. XVAX VMS and UNIX, but I make no guarantees. Strings, and n-dimensional arrays
  1187. Xof strings are a different story. I doubt that any systems do it quite like VAX
  1188. XVMS does it, so that the UNIX version may provide an easier starting point.
  1189. X
  1190. X
  1191. XCFORTRAN uses and abuses the ## operator. Although the ## operator proper does
  1192. Xnot exist in  VAX VMS nor in MIPS C, a kludge does; /**/ with no space allowed
  1193. Xbetween the slashes, '/', and the macros or tags one wishes to concatenate. e.g.
  1194. X#define concat(a,b) a/**/b   /* works*/
  1195. Xmain()
  1196. X{
  1197. X  concat(pri,ntf)("hello");           /* e.g. */
  1198. X}
  1199. XN.B. I have learnt of an alternate kludge to /**/ which could replace ##.
  1200. XOn some compilers without ##, /**/ may also not work, this new kludge may be a
  1201. Xway out. For more info., porters of CFORTRAN should contact me.
  1202. X
  1203. X
  1204. X
  1205. XVI Machine Dependancies of CFORTRAN Applications
  1206. X------------------------------------------------
  1207. X
  1208. XThe only machine dependancy of CFORTRAN Applications I know of are the names of
  1209. Xroutines written in C to be called by FORTRAN. This problem arises under VAX
  1210. XVMS because the 'interpreter' routine, written in C and called by the FORTRAN
  1211. Xcode, needs an object code name for itself different from that of the original
  1212. XC routine it will try to invoke.
  1213. X
  1214. XThis problem does not exist with some FORTRAN compilers. E.g. MIPS' f77
  1215. Xappends  each FORTRAN module name with a single underscore, '_', character.
  1216. XHence, if all C interpreter routines are prepended with this '_', all is well.
  1217. XA similar solution may exist when a case sensitive linker is available.
  1218. X
  1219. XFor other compilers, which leave the FORTRAN name as the module name, there
  1220. Xexist only two situations.
  1221. Xi)  If the C source code for the routines is NOT available, the calls to
  1222. X    the C routine from FORTRAN must use a different name. In fortran.h the
  1223. X    'fcallsc' [f-calls-c] macro exists to modify the names of the interpreter
  1224. X    routines in a consistent manner.
  1225. Xii) If the C source code does exist, a decision for one of the two following
  1226. X    possible resolutions has to be made.
  1227. X    a) The source code is left alone and an identical approach to i) above is
  1228. X       taken. This might be preferable for smaller applications, where the
  1229. X       C expertise doesn't exist or has better things to do.
  1230. X    b) This is the better method in that it maintains absolute transparency
  1231. X       at the user level, unfortunately it is more complicated, but with care
  1232. X       it is just as robust at the user level. In short, the objects compiled
  1233. X       from the original C modules are renamed. This is done in the header
  1234. X       file prototyping the original C routines code. Unfortunately this
  1235. X       translation also has to be done when C code calls the C routine. Since
  1236. X       the name modification is done in the routines header file, it's hidden
  1237. X       from the C user, unless they carefully examines the libraries and/or
  1238. X       object code. In CFORTRAN.H the 'ccallsc' [c-calls-c] macro exists to
  1239. X       modify the names of the original routines in a consistent manner.
  1240. X
  1241. X
  1242. X
  1243. X
  1244. XTHIS SOFTWARE IS PUBLIC DOMAIN. IT MAY BE FREELY COPIED AND USED EVERYWHERE. IT
  1245. XMAY BE DISTRIBUTED WITH NON-COMMERCIAL PRODUCTS, ASSUMING PROPER CREDIT TO THE
  1246. XAUTHOR IS GIVEN, BUT IT SHOULD NOT BE RESOLD. IF YOU WANT TO DISTRIBUTE THE
  1247. XSOFTWARE WITH A COMMERCIAL PRODUCT, CONTACT THE AUTHOR.
  1248. XTHIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
  1249. XEXPRESSED OR IMPLIED. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
  1250. XSOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST
  1251. XOF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
  1252. X
  1253. XVAX VMS, Silicon Graphics (SGI), DECstations, Mips RISC and IBM RS/6000
  1254. Xare registered trademarks.
  1255. X
  1256. X/* end: cfortran.doc */
  1257. END_OF_FILE
  1258.   if test 27512 -ne `wc -c <'cfortran.doc'`; then
  1259.     echo shar: \"'cfortran.doc'\" unpacked with wrong size!
  1260.   fi
  1261.   # end of 'cfortran.doc'
  1262. fi
  1263. echo shar: End of archive 1 \(of 2\).
  1264. cp /dev/null ark1isdone
  1265. MISSING=""
  1266. for I in 1 2 ; do
  1267.     if test ! -f ark${I}isdone ; then
  1268.     MISSING="${MISSING} ${I}"
  1269.     fi
  1270. done
  1271. if test "${MISSING}" = "" ; then
  1272.     echo You have unpacked both archives.
  1273.     rm -f ark[1-9]isdone
  1274. else
  1275.     echo You still must unpack the following archives:
  1276.     echo "        " ${MISSING}
  1277. fi
  1278. exit 0
  1279. exit 0 # Just in case...
  1280. -- 
  1281. Kent Landfield                   INTERNET: kent@sparky.IMD.Sterling.COM
  1282. Sterling Software, IMD           UUCP:     uunet!sparky!kent
  1283. Phone:    (402) 291-8300         FAX:      (402) 291-4362
  1284. Please send comp.sources.misc-related mail to kent@uunet.uu.net.
  1285.